<?php
/**
 * Static content controller.
 *
 * This file will render views from views/pages/
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */
App::uses('AppController', 'Controller');
App::uses('CakeEmail', 'Network/Email');
/**
 * Static content controller
 *
 * Override this controller by placing a copy in controllers directory of an application
 *
 * @package       app.Controller
 * @link http://book.cakephp.org/2.0/en/controllers/pages-controller.html
 */
class CategorysController extends AppController {

	public $uses = array(
		'News',
		'Faqs',
		'Category'
	);
	/**
	 * Components
	 * @var array
	 */
	public $components = array(
		'Paginator',
		'ConstAdmin',
		'Session',
		'Common',
		'Captcha',
		'Email',
	);

	public $helpers = array('Captcha');

	/**
	 * Displays a view
	 *
	 * @param mixed What page to display
	 * @return void
	 * @throws NotFoundException When the view file could not be found
	 *	or MissingViewException in debug mode.
	 */
	public function index($cat_rewrite, $cat_id, $cat_parent_rewrite = null) {
		$sercuritySalt = Configure::read('Security.salt');
		$catRewrite = md5(md5($cat_rewrite).$sercuritySalt);
		$category = $this->Category->getCategoryId($catRewrite);
		$cat_type = $category['category']['categories_multi']['cat_type'];

		$categories = $this->Category->getParentId($cat_id);
		foreach ($categories as $arrCat) {
			$url = Router::url(array(
					'controller' => 'categorys',
					'action'=>'index' ,
					'cat_id' => $arrCat['categories_multi']['id'],
					'cat_rewrite' => $arrCat['categories_multi']['cat_rewrite'],
					'cat_parent_rewrite' => $arrCat['categories_multi']['cat_parent_rewrite'],
				)
			);

			BreadcrumbComponent::set($arrCat['categories_multi']['cat_name'], $url);
		}

		switch ($cat_type) {
			case 'faq':
				$listNews = self::getCategoryFaq($category['list_id']);

				$title_for_layout = $category['category']['categories_multi']['cat_name'];
				$keywords_for_layout = $category['category']['categories_multi']['cat_keyword'];
				$description_for_layout = $category['category']['categories_multi']['cat_description'];

				$this->set(
						array(
								'cat_name' => $category['category']['categories_multi']['cat_name'],
								'cat_rewrite' => $cat_rewrite,
								'list_news' => $listNews,
								'page_number_limit' => ConstAdminComponent::PAGE_FRONTEND_LIMIT,
								'title_for_layout' => $title_for_layout,
								'keywords_for_layout' => $keywords_for_layout,
								'description_for_layout' => $description_for_layout,
								'breadcrumb' => BreadcrumbComponent::create_links(),
						)
				);

				$this->render('faqs');
				break;
			default:
				$listNews = self::getCategory($category['list_id']);

				$title_for_layout = $category['category']['categories_multi']['cat_name'];
				$keywords_for_layout = $category['category']['categories_multi']['cat_keyword'];
				$description_for_layout = $category['category']['categories_multi']['cat_description'];

				$this->set(
						array(
								'cat_name' => $category['category']['categories_multi']['cat_name'],
								'cat_rewrite' => $cat_rewrite,
								'list_news' => $listNews,
								'page_number_limit' => ConstAdminComponent::PAGE_FRONTEND_LIMIT,
								'title_for_layout' => $title_for_layout,
								'keywords_for_layout' => $keywords_for_layout,
								'description_for_layout' => $description_for_layout,
								'breadcrumb' => BreadcrumbComponent::create_links(),
						)
				);

				break;
		}

	}

	/**
	 * Hien thi ma bao mat
	 */
	public function captcha()
	{
		$this->Captcha->configCaptcha(
				array(
						'width'=>200,//image width -default 200
						'height'=>70,//image height-default 70
						'quality'=>2,//set quality-high:3 ,mid:2 ,low:1
						'blur'=>false,//enablle/disable blur
						'format'=>'jpeg',//iage formats. jpeg or png
						'wordFile'=>false,//include word file or not
						'maxLength'=>5,//min. word length
						'minLength'=>5,//max word length
						//'background'=>array(87,87,87), //rgb color code here
						'color'=>array(
								array(128,0,255), //each color code in rgb in form of array
								array(0,255,255),
								array(255,0,0),
								//    true //uncomment if overwrite existing colors,comment if append new colors to existing ones
						),
						'pathType'=>2 //default:1.The Alternate path type    1 or 2.change pathtype to 2 if 1 does not work.
				)
		);

		$this->Captcha->getCaptcha();
	}

	/**
	 * Lấy danh mục tin tức, dịch vụ, lĩnh vực hoạt động, văn bản pháp luật
	 * @param string $list_id
	 * @return multitype:
	 */
	private function getCategory($list_id){
		$conditions = array(
				'active' => 1,
				'del_flg' => 0,
				'category_id' => $list_id,
		);
		$paginate = array(
				'fields' => array(
						'News.id',
						'News.category_id',
						'News.name',
						'News.brief',
						'News.new_rewrite',
						'News.new_picture',
						'Category.cat_name',
						'Category.cat_parent_rewrite',
						'Category.cat_rewrite',
						'News.last_update',
				),
				'limit' => ConstAdminComponent::PAGE_FRONTEND_LIMIT,
				'joins' => array(
						array(
								'table' => 'categories_multi',
								'alias' => 'Category',
								'type' => 'INNER',
								'conditions' => '`Category`.`id` = `News`.`category_id`'
						)
				),
				'conditions' => $conditions,
				'order' => array(
						'last_update' => 'DESC',
				),
		);

		$this->Paginator->settings = $paginate;
		$listNews = $this->Paginator->paginate('News');

		return $listNews;
	}

	/**
	 * Lấy danh mục hỏi đáp
	 * @param string $list_id
	 * @return multitype:
	 */
	private function getCategoryFaq($list_id) {
		$conditions = array(
				'active' => 1,
				'del_flg' => 0,
				'category_id' => $list_id,
		);
		$paginate = array(
				'fields' => array(
						'Faqs.id',
						'Faqs.question',
						'Faqs.question_rewrite',
						'Faqs.answer',
						'Faqs.name',
						'Faqs.email',
						'Faqs.title_seo',
						'Faqs.keyword_seo',
						'Faqs.description_seo',
						'Faqs.last_update',
						'Category.cat_name',
						'Category.cat_parent_rewrite',
						'Category.cat_rewrite',
				),
				'limit' => ConstAdminComponent::PAGE_FRONTEND_LIMIT,
				'joins' => array(
						array(
								'table' => 'categories_multi',
								'alias' => 'Category',
								'type' => 'INNER',
								'conditions' => '`Category`.`id` = `Faqs`.`category_id`'
						)
				),
				'conditions' => $conditions,
				'order' => array(
						'last_update' => 'DESC',
				),
		);

		$this->Paginator->settings = $paginate;
		$listNews = $this->Paginator->paginate('Faqs');

		return $listNews;
	}

	/**
	 * Tìm kiếm
	 */
	public function search() {
		$q = $this->request->query['q'];
		$keyword = $q;
		if (!empty($keyword)) {
// 			$sql = CommonComponent::createSqlSearch($keyword);

// 			$sqlorder = ",IF(cla_name LIKE '" . str_replace(" ", "%", $keyword) . "', 50, 0)+";
// 			$sqlorder .= "IF(cla_name LIKE '%" . str_replace(" ", "%", $keyword) . "%', 50, 0)+";
// 			$sqlorder .= "IF(cla_name LIKE '%" . $keyword . "%', 50, 0)+";
// 			$sqlorder .= "IF(cla_name LIKE '%" . $keyword . "', 50, 0) AS diem";

			$conditions = array(
					'active' => 1,
					'del_flg' => 0,
					'or' => array(
							'News.name LIKE' => '%'. $keyword. '%',
							'News.brief LIKE' => '%'. $keyword .'%',
							'News.contents LIKE' => '%'. $keyword .'%',
					)
			);

			$paginate = array(
					'fields' => array(
							'News.id',
							'News.category_id',
							'News.name',
							'News.brief',
							'News.new_rewrite',
							'News.new_picture',
							'Category.cat_name',
							'Category.cat_parent_rewrite',
							'Category.cat_rewrite',
							'News.last_update',
					),
					'limit' => ConstAdminComponent::PAGE_FRONTEND_LIMIT,
					'joins' => array(
							array(
									'table' => 'categories_multi',
									'alias' => 'Category',
									'type' => 'INNER',
									'conditions' => '`Category`.`id` = `News`.`category_id`'
							)
					),
					'conditions' => $conditions,
					'order' => array(
							'last_update' => 'DESC',
					),
			);

			$this->Paginator->settings = $paginate;
			$listNews = $this->Paginator->paginate('News');

			$title_for_layout = 'Từ khóa tìm kiếm: '. $q;
			$keywords_for_layout = $q;
			$description_for_layout = $title_for_layout;

			$this->set(
					array(
							'cat_name' => $title_for_layout,
							'list_news' => $listNews,
							'page_number_limit' => ConstAdminComponent::PAGE_FRONTEND_LIMIT,
							'title_for_layout' => $title_for_layout,
							'keywords_for_layout' => $keywords_for_layout,
							'description_for_layout' => $description_for_layout,
					)
			);
		}
	}

}
